
const App = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    title: '我的客户',
    currentIndex: 0,
    topData: {},
    pubTopData: {},
    xfPubTopData: {},
    title1: "区域",
    title2: "状态",
    option1: [],
    option2: [],
    value1: '',
    value2: '',
    building_name:'',
    deviceList: [],    //列表
    deviceTotal: 0,    //总数
    devicePages: 0,     //总页数
    deviceIndex: 1,      //当前页 
    searchLoading: false, //加载更多
    deviceNoData: false,  //暂无更多
    searchLoadingComplete: false, //加载到底部
    api_root:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onShow: function () {
    let api_root = wx.getStorageSync('api_root');
    this.getDictionaryData()
    this.getList()
    this.setData({
      title1: "区域",
      title2: "状态",
      api_root:api_root
    })
  },
  onLoad: function (options) {

  },
  handleChange(e) {
    let value = e.detail
    this.data.option1.forEach((item, index) => {
      if (item.value == value) {
        this.setData({
          value1: value,
          title1: item.text == '全部' ? '区域' : item.text
        })
      }
    });
    this.setData({
      deviceList: [],
      deviceIndex: 1
    })
    this.getList(this.data.option1)
  },
  handleChange2(e) {
    let value = e.detail
    this.data.option2.forEach((item, index) => {
      if (item.value == value) {
        this.setData({
          value2: value,
          title2: item.text == '全部' ? '状态' : item.text 
        })
      }
    });
    this.setData({
      deviceList: [],
      deviceIndex: 1
    })
    this.getList(this.data.option2)
  },
  handleInputSearch(e){
    console.log(e)
    let { value } = e.detail
    this.setData({
      building_name:value
    })
  },
  handleSearchList(){
    this.setData({
      deviceList: [],
      deviceIndex: 1
    })
    this.getList(this.data.keywords)
  },
  toAddPage() {
    wx.navigateTo({
      url: '../edit/index?type=add',
    })
  },
  //字典列表重组
  filterList(arr, type) {
    let list = []
    let obj = {
      text: '',
      value: ''
    }
    for (let index = 0; index < arr.length; index++) {
      obj = {
        text: arr[index].value,
        value: arr[index].key
      }
      list.push(obj)
    }
    list.unshift({ text: '全部', value: '' })
    return list

  },
  filterList2(arr, type) {
    let list = []
    let obj = {
      text: '',
      value: ''
    }
    for (let index = 0; index < arr.length; index++) {
      obj = {
        text: arr[index],
        value: arr[index]
      }
      list.push(obj)
    }
    list.unshift({ text: '全部', value: '' })
    return list
    // console.log(arr)
  },

  //字典接口
  getDictionaryData() {
    let _this = this
    App._post_form('Building/getBuildingScreen', {
      keys: '["location_arr","status_arr"]'
    }, function (result) {
      _this.setData({
        option1: _this.filterList2(result.data.location_arr, '区域'),
        option2: _this.filterList(result.data.status_arr, '状态'),
      })
      console.log(_this.data.option1)
    });
  },
  getList() {
    let _this = this
    wx.showLoading({
      title: '加载中...',
    })
    // console.log(_this.data.deviceIndex);
    App._post_form('Building/getBuildingData', {
      page: _this.data.deviceIndex,
      location: _this.data.value1,
      status: _this.data.value2,
      building_name: _this.data.building_name,
    }, function (result) {
      wx.hideLoading()
      _this.filterList(result.data.list)
      let Result = result.data.list
      //     let Result = result.data.list.map(el=>{
      //         const mobile = _this.mobileStr(el.mobile);
      //         return {...el, mobile}
      //      })
      // 响应无数据 则提示无数据
      if (_this.data.deviceIndex == 1 && Result.length == 0) {
        _this.setData({
          deviceNoData: true,
          searchLoading: false,
          searchLoadingComplete: false
        })
      }
      if (Result.length) {
        _this.data.deviceTotal = result.data.count
        _this.data.devicePages = result.data.total_page
        let deviceListTemp = [];
        if (_this.data.deviceIndex > 1) {
          deviceListTemp = _this.data.deviceList.concat(Result);
          console.log(deviceListTemp)
          _this.setData({
            searchLoading: true, //"正在载入更多"显示
          });
        } else {
          deviceListTemp = Result;
        }
        _this.setData({
          deviceList: deviceListTemp, //获取数据数组
          deviceNoData: false //“没有数据”隐藏
        });
      }
    });
  },
  onReady: function () {
    //页面渲染完成 第三运行
    //console.log('onReady');
  },
  /**
     * 下拉刷新
     */
  onPullDownRefresh: function () {
    // 获取首页数据
    this.onShow()
  },
  // 页面触底时执行
  reachBottom(action) {
    var _this = this;
    if (_this.data.deviceIndex < _this.data.devicePages) {
      _this.setData({
        deviceIndex: _this.data.deviceIndex + 1,   //每次触发触底事件，把deviceIndex+1
      });
      _this.getList(action);
      return

    } else if (_this.data.deviceIndex == _this.data.devicePages) {
      _this.setData({
        searchLoadingComplete: true, //“已加载全部”显示
        searchLoading: false, //"正在载入更多"隐藏
      });
    }
  }
})